<template>
    <div>
      <div class="top-panel">
        <el-card>
          <el-form :model="searchForm" label-width="100px" label-position="right">
            <el-row>
              <el-col :span="5">
                <el-form-item label="实验室名称">
                  <el-input
                    class="password-input"
                    v-model="searchForm.nameFuzzy"
                    clearable
                    placeholder="支持模糊搜索"
                    @keyup.enter="loadDataList"
                  >
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="5">
                <el-form-item label="具体地址">
                  <el-input
                    class="password-input"
                    v-model="searchForm.locationFuzzy"
                    clearable
                    placeholder="支持模糊搜索"
                    @keyup.enter="loadDataList"
                  >
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="5">
                <el-form-item label="联系人">
                  <el-input
                    class="password-input"
                    v-model="searchForm.contactFuzzy"
                    clearable
                    placeholder="支持模糊搜索"
                    @keyup.enter="loadDataList"
                  >
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="9" :style="{ paddingLeft: '10px' }">
                <el-button type="success" @click="loadDataList()"
                  >查询
                </el-button>
                <el-button
                  type="primary"
                  @click="showEdit()"
                  >新增实验室
                </el-button>
              </el-col>
            </el-row>
          </el-form>
        </el-card>
      </div>
      <el-card class="table-data-card">
        <template #header>
          <span>实验室列表</span>
        </template>
        <Table
          ref="tableInfoRef"
          :columns="columns"
          :fetch="loadDataList"
          :dataSource="tableData"
          :options="tableOptions"
          :extHeight="tableOptions.extHeight"
        >

          <template #slotOperation="{ index, row }">
            <div
              class="row-op-panel"

            >
              <a
                class="a-link"
                href="javascript:void(0)"
                @click="showEdit(row)"
                >编辑</a
              >

              <a
                class="a-link"
                href="javascript:void(0)"
                @click.prevent="delLaboratory(row)"
                >删除</a
              >

            </div>
          </template>
        </Table>
      </el-card>
      <LaboraryEdit ref="laboraryEditRef" @reload="loadDataList"></LaboraryEdit>
    </div>
  </template>

  <script setup>
  import LaboraryEdit from "./LaboraryEdit.vue"
  import { getCurrentInstance, nextTick, ref } from "vue";
  const { proxy } = getCurrentInstance();

  const userInfo = ref(proxy.VueCookies.get("userInfo") || {});

  const api = {
    loadDataList: "/laboratory/loadLaboratoryList4Admin",
    delLaboratory: "/laboratory/delLaboratory",
    saveReservation:"/reservationInfo/saveReservation",
  };
  const searchForm = ref({});

  const tableData = ref({});
  const tableOptions = ref({
    extHeight: 125,
  });

  const columns = [
    {
      label: "实验室编号",
      prop: "laboratoryId",
      width:130,
    },
    {
      label: "实验室名称",
      prop: "name",
    },
    {
      label: "地址",
      prop: "location",
    },
    {
      label: "联系方式",
      prop: "contact",
    },
    {
      label: "实验室管理员",
      prop: "userName",
    },
    {
      label: "操作",
      prop: "operation",
      width: 250,
      scopedSlots: "slotOperation",
    },
  ];

  const tableInfoRef = ref();

  const loadDataList = async () => {
    let params = {
      pageNo: tableData.value.pageNo,
      pageSize: tableData.value.pageSize,
    };
    Object.assign(params, searchForm.value);
    let result = await proxy.Request({
      url: api.loadDataList,
      params: params,
    });
    if (!result) {
      return;
    }
    Object.assign(tableData.value, result.data);
  };

  //删除
  const delLaboratory = (data) => {
    proxy.Confirm(`确定要删除【${data.title}】吗？`, async () => {
      let result = await proxy.Request({
        url: api.delLaboratory,
        params: {
          id: data.laboratoryId,
        },
      });
      if (!result) {
        return;
      }
      proxy.Message.success("操作成功");
      loadDataList();
    });
  };

  //预约图书
  const saveReservation = (data) => {

    proxy.Confirm(`确定要【预约】【${data.title}】这本书吗？`, async () => {
      let result = await proxy.Request({
        url: api.saveReservation,
        params: {
          bookId: data.bookId,
        },
      });
      if (!result) {
        return;
      }
      proxy.Message.success("预约成功，请前往图书管理》图书预约查询记录，并查看预约须知");
      loadDataList();
    });
  };

  const laboraryEditRef = ref();
  const showEdit = (data = {}) => {
    laboraryEditRef.value.showEdit(Object.assign({}, data));
  };


  </script>

  <style lang="scss" scoped>
  .detail-tree-panel {
    height: calc(100vh - 273px);
    overflow: auto;
    width: 100%;
  }
  </style>
